package com.xinongbest.test.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author lemonwan
 * JDK动态代理
 */
public class PersonProxy implements InvocationHandler {
    private Person person;

    public PersonProxy(Person person) {
        this.person = person;
    }

    public Person proxyAction() {
        return (Person) Proxy.newProxyInstance(person.getClass().getClassLoader(), person.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("代理方法之前执行");
        Object invoke = method.invoke(person, args);
        System.out.println("代理方法之后执行");
        return invoke;
    }
}
